﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Tizer.Web.Backoffice
{
    /// <summary>
    /// Базовая страница администратора
    /// </summary>
    public class AdminBasePage : Page
    {
        // путь от корня
        public const string RootUrl = "~/Backoffice/";
        public const string RootLogoUrl = "~/store/logo/";
        public const string RootMapUrl = "~/store/map/";

        // начальные страницы
        public const string UrlAdminDashboard =  RootUrl + "Dashboard.aspx";
        public const string UrlHome = "~/";

        // страницы аккаунтов
        public const string UrlAdminAccountList = RootUrl + "AccountList.aspx";
        public const string UrlAdminAccountAdd = RootUrl + "AccountAdd.aspx";
        public const string UrlAdminAccountEdit = RootUrl + "AccountEdit.aspx";
        public const string UrlAdminAccountDetail = RootUrl + "AccountDetail.aspx";

        //страницы стран
        public const string UrlAdminCountryList = RootUrl + "CountryList.aspx";
        public const string UrlAdminCountryAdd = RootUrl + "CountryAdd.aspx";
        public const string UrlAdminCountryEdit = RootUrl + "CountryEdit.aspx";
        public const string UrlAdminCountryDetail = RootUrl + "CountryDetail.aspx";
        
        //страницы городов
        public const string UrlAdminCityList = RootUrl + "CityList.aspx";
        public const string UrlAdminCityAdd = RootUrl + "CityAdd.aspx";
        public const string UrlAdminCityEdit = RootUrl + "CityEdit.aspx";
        public const string UrlAdminCityDetail = RootUrl + "CityDetail.aspx";            
        
        //страницы валюты
        public const string UrlAdminCurrencyList = RootUrl + "CurrencyList.aspx";
        public const string UrlAdminCurrencyAdd = RootUrl + "CurrencyAdd.aspx";
        public const string UrlAdminCurrencyEdit = RootUrl + "CurrencyEdit.aspx";
        public const string UrlAdminCurrencyDetail = RootUrl + "CurrencyDetail.aspx";        
        
        //страницы услуг
        public const string UrlAdminServiceList = RootUrl + "ServiceList.aspx";
        public const string UrlAdminServiceAdd = RootUrl + "ServiceAdd.aspx";
        public const string UrlAdminServiceEdit = RootUrl + "ServiceEdit.aspx";
        public const string UrlAdminServiceDetail = RootUrl + "ServiceDetail.aspx";        
        
        //страницы типов услуг
        public const string UrlAdminServiceTypeList = RootUrl + "ServiceTypeList.aspx";
        public const string UrlAdminServiceTypeAdd = RootUrl + "ServiceTypeAdd.aspx";
        public const string UrlAdminServiceTypeEdit = RootUrl + "ServiceTypeEdit.aspx";
        public const string UrlAdminServiceTypeDetail = RootUrl + "ServiceTypeDetail.aspx";

        // сообщения
        public const string StrNotChoosed = "- не указано -";

        /// <summary>
        /// Установить заголовки и описание страницы
        /// </summary>
        /// <returns></returns>
        public void SetPageTitle()
        {
            // главный заголовок страницы
            var siteMapProvider = SiteMap.Providers["TizerAdminSiteMapProvider"];
            if (siteMapProvider != null && siteMapProvider.CurrentNode != null)
            {
                var header = FindControl(this.Page, "lblPageHeader") as HtmlGenericControl;
                if (header != null)
                    header.InnerText = siteMapProvider.CurrentNode.Title;

                var pageDesc = FindControl(this.Master, "lblPageDescription") as HtmlGenericControl;
                if (pageDesc != null)
                    pageDesc.InnerText = siteMapProvider.CurrentNode.Description;

                var title = FindControl(this.Master, "lblTitle") as HtmlGenericControl;
                if (title != null)
                    title.InnerText = WebConfigurationManager.AppSettings["ApplicationName"] + " : " + siteMapProvider.CurrentNode.Title;
            }
        }

        /// <summary>
        /// Поиск котрола на странице
        /// </summary>
        /// <param name="control">Контрол, содержащий нужный контрол</param>
        /// <param name="id">ID контрола</param>
        /// <returns>Контрол</returns>
        public static Control FindControl(System.Web.UI.Control control, string id)
        {
            Control findedControl = control.FindControl(id);
            if (findedControl != null)
                return findedControl;
            foreach (Control c in control.Controls)
            {
                findedControl = FindControl(c, id);
                if (findedControl != null)
                    return findedControl;
            }
            return null;
        }
    }
}